package com.device.service;

import com.device.entity.AliyunOssResult;
import com.device.enums.FileType;
import com.device.util.AliyunOSSUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@Service
@AllArgsConstructor
public class FileUploadService {
    private final AliyunOSSUtil aliyunOSSUtil;

    public AliyunOssResult uploadFile(MultipartFile file, FileType fileType) {
        String suffix = Objects.requireNonNull
                (file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        String objectName = fileType.name().toLowerCase() + "/" + UUID.randomUUID() + "." + suffix;
        try {
            return aliyunOSSUtil.upload(file.getInputStream(), objectName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
